package com.beermanager.net;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.json.JSONException;

import com.beermanager.googleapi.place.GooglePlaceParser;
import com.beermanager.googleapi.place.PlaceEntry;

public class DownloadBarsTask extends Downloader<Void, Void, List<PlaceEntry>>{

	public static final String BARS_URL = "https://maps.googleapis.com/maps/api/place/search/json?location=-30.039015,-51.224045&radius=500&types=food&&sensor=false&key=AIzaSyDVjSZpEwLv4AZuJhtvv92N-syvZ4ZQUZU";
	//public static final String BARS_URL = "https://maps.googleapis.com/maps/api/place/search/json?location=-30.072901,-51.119835&radius=500&types=food&&sensor=false&key=AIzaSyDVjSZpEwLv4AZuJhtvv92N-syvZ4ZQUZU";	
	
	private List<PlaceEntry> loadFromGooglePlacesApi(String url)  {
		
		InputStream inputStream = null;
		GooglePlaceParser parser = new GooglePlaceParser();
		
		try {        	
			inputStream = downloadUrl(url);
			System.out.println("Download Places Completed.");
			return parser.parse(inputStream);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;		
	}
	
	@Override
	protected List<PlaceEntry> doInBackground(Void ... params) {
		 try {
             return loadFromGooglePlacesApi(BARS_URL);
         } catch (Exception e) {
        	 e.printStackTrace();
        	 return null;
         }
	}	
}
